Ext.namespace("NewCustomer");

NewCustomer.ToReport = function() {
	this.winEdit = null;
	this.winMsg=null;
	this.remind=null;	
	
	this.storeType  = new Ext.data.ArrayStore({
        	fields: ['value', 'name'],
        	data: OMS.FILE_TYPE2
    });
	this.txtIssueType = new Ext.form.ComboBox({
		width: 150,
		name:'issueType',
		store: this.storeType,
        valueField: 'value',
        displayField: 'name',
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true
	});
	this.dtStart = new Ext.form.DateField({
		width: 85,
		name : 'issueDateFm',
		format: 'Y-m-d'
	});
	this.dtEnd = new Ext.form.DateField({
		width: 85,
		name : 'issueDateTo',
		format: 'Y-m-d'
	});
	this.btnNew = new Ext.Button({
		text: '新建下载资料',
		iconCls: 'icon-newplan',
		scope: this,
		handler: this.onNew
	});
	this.btnModify = new Ext.Button({
		text: '修改下载资料',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onModify
	});
	this.btnSubmit = new Ext.Button({
		text: '选择公司客户',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onSubmit
	});
	this.btnDelete = new Ext.Button({
		text: '删除下载资料',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onDelete
	});
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找下载资料'},
        handler: this.queryIssue,
        scope: this
    });
		
	this.store = new Ext.data.JsonStore({
	    //url: '../superviseIssue/toReportIssue.form?timesnap=' + Date.parse(new Date()),
	    url : '../customer/searchCustomer.form?status=11&timesnap=' + Date.parse(new Date()),
	    root: 'result',
	    totalProperty: 'total',
        remoteSort: true,
        fields: ['id', 'CustomerName', 'Area',
				 'CustomerSize', 'CustomerType', 'CustomerAddress',
				 'FirstContact', 'Mobile', 'Leader', 'EmployCount',
				 'CompanyPhone', 'MSN', 'Email', 'Fax',
				 'ContactLike', 'Birthday', 'SecondContact',
				 'SecondMobile', 'PhoneMoney', 'MailMoney', 'NetMoney',
				 'SuccessTime', 'Stage', 'VsName',
				 'NsManager', 'VsTime', 'VsGood', 'VsBad',
				 'CreateTime','CreateBy','LastUpdateTime','LastUpdateBy'
        		],
        listeners : {
					scope : this,
					beforeload : function(thiz) {
					thiz.baseParams = {
						stage : 0}
						}
					}
	});
	//this.store.setDefaultSort('DocId', 'desc');
	this.renderType = function(value){
		return OMS.FILE_TYPE[value];
	};
	this.renderLevel = function(value){
		return OMS.FILE_LEVEL[value];
	};
	this.renderStatus = function(value){
		return OMS.SUPERVISE_STATE[value];
	};
	this.sm =  new Ext.grid.CheckboxSelectionModel();
	this.column = [
		this.sm,
		 {header:'客户名称',width: 150,sortable: true,dataIndex:'CustomerName'},
         {header:'所属区域',width: 100, sortable: true,dataIndex:'Area',renderer:function(value){return OMS.AREA[value];}},
         {header:'项目类型',dataIndex:'CustomerType',renderer:function(value,row){return OMS.CUSTOMERTYPE[value];}},
         {header:'公司地址',dataIndex:'CustomerAddress'},
         {header:'联系人姓名',width: 170,dataIndex:'FirstContact'},
         {header:'联系人电话',dataIndex:'Mobile'}
	];
			
	NewCustomer.ToReport.superclass.constructor.call(this, {
		title: '公司数据库',
        region: 'center',
        cm: new Ext.grid.ColumnModel(this.column),
        sm: this.sm,
        tbar: [//this.btnNew,this.btnModify,this.btnDelete,
        		this.btnSubmit,
        		'->','资料类别:', this.txtIssueType, 
		          '上传时间:', this.dtStart, '~', this.dtEnd,this.btnSearch],
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        store:this.store,
        viewConfig: { forceFit: true },
        listeners:{
        	//rowdblclick : this.onView,//双击网格
			//render:this.queryIssue
			activate: this.queryIssue
		}
    });
};
Ext.extend(NewCustomer.ToReport, Ext.grid.GridPanel, {
	queryIssue:function(btn,event){
		var param={
			start : 0,
			limit : 15,
			issueDateFm:this.dtStart.getValue(),
			issueDateTo:this.dtEnd.getValue(),
			issueType:this.txtIssueType.getValue()
		};
		this.store.load({
			params:param
		});
	},
	onNew: function(btn, event) {
		if (this.winEdit == null) {
			this.winEdit = new NewCustomer.Edit(this);
		}
		this.winEdit.show();
		this.winEdit.initWin();		
	},
	onModify: function(btn, event) {
		if (this.winEdit == null) {
			this.winEdit = new NewCustomer.Edit(this);
		}
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn2('请选择一条记录！',180);
			return ;
		}else if(records.length>1){
			Utils.warn2('只能选择一条记录！',180);
			return;
		}
		this.winEdit.show();
		this.winEdit.initWin(records[0].data);		
	},
	onSubmit: function(btn, event) {
		/**原来的内容
		scope=this;
		if(this.winMsg==null)
			this.winMsg=new Message.Msg(function(){
				scope.showSubmit();
				//alert(scope.remind);
			},'上报后就不可修改，你确实要上报吗？');
		this.winMsg.show();	
		**/
		var records = this.getSelectionModel().getSelections();
		var ids="";
		if(records.length==0){
			Utils.warn2('请选择一条记录！',180);
			return ;
		}
		for(var i=0 ;i<records.length;i++){
			var data = records[i].data['id'];
			ids+=records[i].data['id']+',';
			}
		var url = '../customer/chooseCustomer.form?timesnap=' + Date.parse(new Date())+'&ids='+ids;
			scope=this;
			Utils.confirm2({
				msg:'是否选择公司客户?',
				width:250,
				fn:function(){
		       		scope.submitIssue(url)
		       		Utils.info("操作成功","系统提示");	
					scope.queryIssue();
			       		success:function(){
							alert('success');
						}
		       }		
		})
	},
	showSubmit:function(){
		var url='../superviseIssue/submitIssue.form?timesnap=' + Date.parse(new Date());
		this.remind=this.winMsg.txtRemind;	
		this.submitIssue(url);
	},
	submitIssue:function(url){
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn2('请选择一条记录！',180);
			return ;
		}
		var ids='';
		for(var i=0;i<records.length;i++){
			ids+=records[i].data['superviseId']+',';
		}
		scope=this;
		Ext.Ajax.request({
		   url: url,
		   params: {remind:this.remind,ids:ids},
		   success: function(){
			Utils.info2();
		   	scope.queryIssue();
		   }
		});
	},	
	onDelete: function(btn, event) {
		//var url='../superviseIssue/deleteIssue.form?timesnap=' + Date.parse(new Date());
		var url = '../customer/searchCustomer.form?status=11&timesnap=' + Date.parse(new Date());
	   
		scope=this;
		Utils.confirm2({
			msg:'删除后将不可恢复，你确实要删除吗?',
			width:250,
			fn:function(){
	       		scope.submitIssue(url)
	       }
	    });
	},
	onView:function(grid, rowIndex,e){		
		if (this.winEdit == null) {
			this.winEdit = new NewCustomer.Edit(this);
		}
		this.winEdit.show();
		this.winEdit.initWin(grid.store.getAt(rowIndex).data);	
	}
});
